package com.yimils.core.hardware.beanUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yimils.core.hardware.entity.GridEntity;
import com.yimils.core.hardware.entity.RetailEntity;
import com.yimils.core.hardware.service.GridService;
import com.yimils.core.hardware.vo.RetailVo;
import com.yimils.core.sys.entity.UserEntity;
import com.yimils.core.sys.entity.UserRoleEntity;
import com.yimils.core.sys.entity.UserRppEntity;
import com.yimils.core.sys.service.UserRoleService;
import com.yimils.core.sys.service.UserRppService;
import com.yimils.core.sys.service.UserService;
import com.yimils.core.sys.vo.FeederVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Component
public class RetailVoBeanUtils {

    @Resource(name = "userRoleService")
    private UserRoleService userRoleService;

    @Resource(name = "userService")
    private UserService userService;

    @Resource(name = "userRppService")
    private UserRppService userRppService;

    @Resource(name = "gridService")
    private GridService gridService;

    public IPage<RetailVo> deepClone(IPage<RetailEntity> src) {
        IPage<RetailVo> pages = new Page<>();
        pages.setCurrent(src.getCurrent());
        pages.setSize(src.getSize());
        pages.setTotal(src.getTotal());
        pages.setPages(src.getPages());
        pages.setRecords(cloneRecords(src.getRecords()));
        return pages;
    }


    public List<RetailVo> cloneRecords(List<RetailEntity> src) {
        List<RetailVo> recordsVo = new ArrayList<>();
        for (RetailEntity entity : src) {
            recordsVo.add(makeVoNode(entity));
        }
        return recordsVo;
    }

    public RetailVo makeVoNode(RetailEntity entity) {
        if (entity != null) {
            RetailVo vo = new RetailVo();
            vo.setId(entity.getId());
            vo.setName(entity.getName());
            vo.setQrCodeUrl(entity.getQrCodeUrl());
            vo.setOnline(entity.getOnline());
            vo.setBattery(entity.getBattery());
            vo.setSn(entity.getSn());
            vo.setBtAddr(entity.getBtAddr());
            UserEntity partner = userService.getById(entity.getPartnerId());
            if (partner != null) {
                vo.setPartnerName(partner.getNick());
            }
            UserEntity feeder = userService.getById(entity.getFeederId());
            if (feeder != null) {
                vo.setFeederName(feeder.getNick());
            }
            UserRoleEntity hotel = userRoleService.getRoleInType(entity.getHotelId(), UserRoleService.UR_HOTEL);
            if (hotel != null) {
                vo.setHotelName(hotel.getHotelName());
            }
            vo.setRoom(entity.getRoom());
            vo.setTotalSales(0L);
            vo.setTotalCount(0L);
            QueryWrapper<GridEntity> qw = new QueryWrapper<>();
            qw.lambda().eq(GridEntity::getHardwareId, entity.getId());
            List<GridEntity> grids = gridService.list(qw);
            List<RetailVo.Grid> gridsVo = new ArrayList<>();
            vo.setGridStatus(gridsVo);
            for (GridEntity grid : grids) {
                RetailVo.Grid gridVo = new RetailVo.Grid();
                gridVo.setCode(grid.getCode());
                gridVo.setState(grid.getState());
                vo.getGridStatus().add(gridVo);
            }
            return vo;
        }
        return null;
    }
}
